/*
 * @Author: 葛文杰
 * @Date: 2021/2/20 11:32
 * @Description: egg controller 数据源字段
 */

'use strict';

const BaseController = require('./base');


class DataSourceFieldInfoController extends BaseController {

    /**
     * 根据数据源id获取数据源字段列表信息
     */
    async getFieldListByDataSourceConfigId() {
        const { ctx, service } = this;
        const { body: dataSourceFieldCondition } = ctx.request;
        let dataSourceFieldArray = await service.dataSourceFieldInfo.getFieldListByDataSourceConfigId(dataSourceFieldCondition);
        this.success({ data: dataSourceFieldArray });
    }

    /**
     * 根据数据源字段id获取数据源字段信息
     */
    async getFieldInfoById() {
        const { ctx, service } = this;
        const { dataSourceFieldId } = ctx.request.body;
        let dataSourceFieldInfo = await service.dataSourceFieldInfo.getFieldInfoById(dataSourceFieldId);
        this.success({ data: dataSourceFieldInfo });
    }

    /**
     * 获取显示字段数组
     * @return {Promise<void>}
     */
    async getShowFieldArrayByType() {
        const { ctx, service } = this;
        const { dataSourceFieldId } = ctx.request.body;
        let dataSourceShowFieldArray = await service.dataSourceFieldInfo.getShowFieldArrayByType(dataSourceFieldId);
        this.success({ data: dataSourceShowFieldArray });
    }

    /**
     * 保存数据源字段信息
     */
    async insertDataSourceFieldInfo() {
        const { ctx, service } = this;
        const {
            body: dataSourceFieldCondition
        } = ctx.request;
        let result = await service.dataSourceFieldInfo.insertDataSourceFieldInfo(dataSourceFieldCondition);
        this.success({ data: result });
    }

    /**
     * 更改数据源字段信息
     */
    async updateDataSourceFieldInfoById() {
        const { ctx, service } = this;
        const {
            dataSourceFieldId,
            fieldUseType,
            fieldName,
            fieldAnotherName,
            fieldType,
            fieldLength
        } = ctx.request.body;
        let result = await service.dataSourceFieldInfo.updateDataSourceFieldInfoById(
          dataSourceFieldId,
          fieldUseType,
          fieldName,
          fieldAnotherName,
          fieldType,
          fieldLength
        );
        this.success({ data: result });
    }

    /**
     * 删除数据源字段信息
     */
    async deleteDataSourceFieldInfoById() {
        const { ctx, service } = this;
        const {
            dataSourceFieldId,
        } = ctx.request.body;
        let result = await service.dataSourceFieldInfo.deleteDataSourceFieldInfoById(
          dataSourceFieldId
        );
        this.success({ data: result });
    }
}

module.exports = DataSourceFieldInfoController;
